package com.leesk.trial.springbootdatastatistics.controller;

import com.leesk.trial.springbootdatastatistics.pojo.Country;
import com.leesk.trial.springbootdatastatistics.pojo.Province;
import com.leesk.trial.springbootdatastatistics.service.CoronaVirusDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: leesk
 * @Description:
 * @Date: Create in 21:45 2020/11/18
 */
//ajax跨域（另一种方法：写一个配置）
@CrossOrigin
@RestController
public class CsvController {
    @Autowired
    CoronaVirusDataService coronaVirusDataService;

    @GetMapping("/test")
    public List<Country> test(Model model) {
        List<Country> countryList = new ArrayList<>();
        countryList = coronaVirusDataService.getCountries();
       //  统计全世界确诊总数
//        int totalConfirmed = countryList.parallelStream().mapToInt(Country::getConfirmed).sum();
//        //  统计全世界累计治愈
//        int totalRecovered = countryList.parallelStream().mapToInt(Country::getRecovered).sum();
//        //  统计全世界累计死亡病例
//        int totalDeaths = countryList.parallelStream().mapToInt(Country::getDeaths).sum();
//        System.out.println(totalConfirmed + " " + totalRecovered + " " + totalDeaths);
//        model.addAttribute("totalConfirmed",totalConfirmed);
//        model.addAttribute("totalRecovered",totalRecovered);
//        model.addAttribute("totalDeaths",totalDeaths);
        return countryList;
    }

    @GetMapping("/find/countryName")
    public List<Province> getProvinceByCountry(@RequestParam String countryName){
        if(StringUtils.isEmpty(countryName)){
            return null;
        }
        return coronaVirusDataService.getProvince(countryName);
    }

//    @GetMapping("/home")
//    public List<RegionStats> home(Model model, String country) {
//        List<RegionStats> regionStatsList;
//        if (country == null || country.equals("all"))
//            regionStatsList = coronaVirusDataService.getAllRegionStats();
//        else
//            regionStatsList = coronaVirusDataService.getAllRegionStats()
//                    .parallelStream()
//                    .filter(locationStates -> locationStates.getCountry().equals(country))
//                    .collect(Collectors.toList());
//
//        //  统计全世界确诊总数
//        int totalConfirmed = regionStatsList.parallelStream().mapToInt(RegionStats::getConfirmed).sum();
//        //  统计全世界累计治愈
//        int totalRecovered = regionStatsList.parallelStream().mapToInt(RegionStats::getRecovered).sum();
//        //  统计全世界累计死亡病例
//        int totalDeaths = regionStatsList.parallelStream().mapToInt(RegionStats::getDeaths).sum();
//
//
//    }
}
